﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace ProjectSem3.Admin
{
    public partial class WebForm10 : System.Web.UI.Page
    {
       
        private dataDataContext dc = new dataDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Employee us = (Employee)Session["user"];
            if (!Page.IsPostBack)
            {
                if (us != null && us.RoleEmID == 1)
                {
                    if (Request.QueryString["id"] != null)
                    {
                        try
                        {
                             Employee emp = this.dc.Employees.Single(c => c.EmployeeID == Convert.ToInt32(Request.QueryString["ID"].ToString()));
                            txtUser.Text = emp.Username;
                            this.txtFullname.Text = emp.FullName;
                            this.TextBoxMail.Text = emp.Email;
                            this.TextBoxAddress.Text = emp.Address;
                            this.TextBoxPhone.Text = emp.Phone;
                            this.TextBoxSalary.Text = Convert.ToDouble(emp.Salary).ToString();
                            if (emp.Status == true && emp.Status != null)
                            {
                                Radio1.Checked = true;
                            }
                            else
                                Radio2.Checked = true;
                            if (emp.Gender == true && emp.Gender != null)
                            {
                                rdoMale.Checked = true;
                            }
                            else
                                rdoFemale.Checked = true; txtBirthday.Text = emp.BirthDay.Value.ToShortDateString();
                            
                        }
                          
                        catch (Exception)
                        {
                            Response.Redirect("Default.aspx");
                        }
                    }
                   
                }
                else
                    Button1.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var list = dc.Employees;
            Employee emp = this.dc.Employees.Single(c => c.EmployeeID == Convert.ToInt32(Request.QueryString["ID"].ToString()));
            if (emp.Username.Equals(txtUser.Text) || list.Where(d => d.Username.Equals(txtUser.Text)).ToList().Count == 0)
                if (emp.Email.Equals(TextBoxMail.Text) || list.Where(d => d.Email.Equals(TextBoxMail.Text)).ToList().Count == 0)
                {
                    emp.Username = txtUser.Text;
                    emp.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBoxpassword.Text, "sha1");
                    emp.FullName = this.txtFullname.Text;
                    emp.Email = this.TextBoxMail.Text;
                    emp.Address = this.TextBoxAddress.Text;
                    emp.Phone = this.TextBoxPhone.Text;
                    emp.Salary = Convert.ToDouble(this.TextBoxSalary.Text);
                    emp.Status = Radio1.Checked ? Radio1.Checked : false;
                    emp.BirthDay = DateTime.Now;
                    emp.Status = rdoMale.Checked;
                    this.dc.SubmitChanges();
                    Response.Redirect("ListEmployee.aspx");
                }
                else
                    lblEmailMessage.Text = "Email is exists";
            else
                LabelMessage.Text = "username is exists";
                
        }
    }

}